﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using QSHC.QSchedule.Silverlight.Helpers;
using QSHC.QSchedule.Web.Models;
using System.Globalization;
using QSHC.QSchedule.Silverlight.PatientDataService;
using QSHC.QSchedule.Silverlight.ViewModels;

namespace QSHC.QSchedule.Silverlight.Views
{
    public partial class BookingAppointmentPage : Page
    {
        public BookingAppointmentPage()
        {
            InitializeComponent();

            SetBlackoutDates();

            LocationComboBox.SelectionChanged += (s, e) => SetBlackoutDates();
            PhysicianACCB.SelectionChanged += (s, e) => SetBlackoutDates();
            ScheduleCalendar.DisplayDateChanged += (s, e) => SetBlackoutDates();

            HNTextBox.KeyUp += (s, e) =>
            {
                var vm = Resources["bookingAppointmentViewModel"] as BookingAppointmentViewModel;

                if (HNTextBox.Text.Length == 8) // ค้นหาถ้าความยาวได้แปดตัวพอดี
                {
                    var client = new PatientDataServiceClient();
                    client.GetPatientDatasByHNCompleted += (s2, e2) =>
                    {
                        vm.SelectedPatient = e2.Result.FirstOrDefault();
                    };
                    client.GetPatientDatasByHNAsync(HNTextBox.Text, PatientDataMode.Address);
                }
                else
                    vm.SelectedPatient = null;
            };

            SearchButton.Click += (s, e) =>
            {
                int datecount = 0; ;
                var value = int.Parse(((ComboBoxItem)AppointmentValueComboBox.SelectedItem).Content.ToString());
                DateTime targetDate;
                switch (AppointmentTypeComboBox.SelectedIndex)
                {
                    case 0: // วัน
                        targetDate = DateTime.Now.AddDays(value);
                        break;
                    case 1: // สัปดาห์
                        targetDate = DateTime.Now.AddDays(value * 7);
                        datecount = value;
                        break;
                    case 2: // เดือน
                        //targetDate = DateTime.Now.AddMonths(value);
                        targetDate = DateTime.Now.AddDays(28 * value); //Daeng
                        break;
                    case 3: // ปี
                        targetDate = DateTime.Now.AddYears(value);
                        break;
                    default:
                        targetDate = DateTime.Now;
                        break;
                }

                var location = LocationComboBox.SelectedValue as Service;
                var physician = PhysicianACCB.SelectedItem as Employee;

                int i = 0;
                while (!CalendarHelper.IsAvailableDate(targetDate.Date, location, physician))
                {
                    targetDate = targetDate.AddDays(1);
                    i++;
                }
                ScheduleCalendar.SelectedDate = targetDate;

                if (ScheduleCalendar.SelectedDate != null)
                {
                    ScheduleCalendar.DisplayDate = (DateTime)ScheduleCalendar.SelectedDate;
                }
            };
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void SetBlackoutDates()
        {
            var location = LocationComboBox.SelectedValue as Service;
            var physician = PhysicianACCB.SelectedItem as Employee;

            ScheduleCalendar.BlackoutDates.Clear();
            var blackoutDates = CalendarHelper.GetBlackoutDates(location, physician, ScheduleCalendar.DisplayDate);

            if (ScheduleCalendar.SelectedDate != null)
            {
                if (blackoutDates.Where(p => p.Date == ScheduleCalendar.SelectedDate.Value.Date).Count() > 0)
                    ScheduleCalendar.SelectedDate = null;
            }

            foreach (var item in blackoutDates)
            {
                ScheduleCalendar.BlackoutDates.Add(new CalendarDateRange(item));
            }
        }
    }
}
